/**
 * 
 */
package wpmp.portal.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import wcommons.httputils.HttpUtils;
import wcommons.httputils.quickbean.RespInfo;
import wpmp.security.client.SecurityConfig;
import wpmp.utils.quickbean.User;
import wpmp.utils.quickbean.UserView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:49:02 AM Dec 5, 2013
 */
@Component
public class UserService {

	protected final Logger log = LoggerFactory.getLogger(getClass());

	private static final String LOGIN = "user.do?action=login";
	private static final String UPDATE_PASSWORD = "user.do?action=updatePassword";
	private static final String LOGOUT = "user.do?action=logout";
	private static final String GET_USER_VIEW = "resource.do?action=getUserView";

	/**
	 * 用户登录
	 * @param loginName
	 * @param password
	 * @param expDays
	 * @return
	 */
	public RespInfo<User> login(final String loginName, final String password, final Integer expDays) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginName", loginName);
		params.put("password", Base64.encodeBase64URLSafeString(password.getBytes()));
		params.put("expDays", expDays);
		final String result = HttpUtils.postForString(SecurityConfig.getAuthUrl(LOGIN), params);
		if (StringUtils.isBlank(result)) {
			log.warn("can not get anything from RemoteServer for login by " + JSON.toJSONString(params));
			return null;
		}
		return JSON.parseObject(result, new TypeReference<RespInfo<User>>() {
		});
	}

	/**
	 * 修改用户密码
	 * @param loginName
	 * @param oldPassword
	 * @param newPassword
	 * @param securityId
	 * @return
	 */
	public RespInfo<Object> updatePassword(String loginName, String oldPassword, String newPassword, String securityId) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginName", loginName);
		// 密码需要base64
		params.put("oldPassword", Base64.encodeBase64URLSafeString(oldPassword.getBytes()));
		params.put("newPassword", Base64.encodeBase64URLSafeString(newPassword.getBytes()));
		params.put("securityId", securityId);
		final String result = HttpUtils.postForString(SecurityConfig.getAuthUrl(UPDATE_PASSWORD), params);
		if (StringUtils.isBlank(result)) {
			log.warn("can not get anything from RemoteServer for updatePassword by " + JSON.toJSONString(params));
			return null;
		}
		return JSON.parseObject(result, new TypeReference<RespInfo<Object>>() {
		});
	}

	/**
	 * 用户登出
	 * @param loginName
	 * @param securityId
	 * @return
	 */
	public RespInfo<Object> logout(String loginName, String securityId) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginName", loginName);
		params.put("securityId", securityId);
		final String result = HttpUtils.postForString(SecurityConfig.getAuthUrl(LOGOUT), params);
		if (StringUtils.isBlank(result)) {
			log.warn("can not get anything from RemoteServer for logout by " + JSON.toJSONString(params));
			return null;
		}
		return JSON.parseObject(result, new TypeReference<RespInfo<Object>>() {
		});
	}

	/**
	 * 获取用户权限视图
	 * @param securityId
	 * @return
	 */
	public RespInfo<UserView> getUserView(String securityId) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("securityId", securityId);
		final String result = HttpUtils.postForString(SecurityConfig.getAuthUrl(GET_USER_VIEW), params);
		if (StringUtils.isBlank(result)) {
			log.warn("can not get anything from RemoteServer for getUserView by " + JSON.toJSONString(params));
			return null;
		}
		return JSON.parseObject(result, new TypeReference<RespInfo<UserView>>() {
		});
	}
}
